package com.dalin.platform.autocode.action;

import java.util.ArrayList;
import java.util.List;

import com.dalin.common.utils.StringUtils;
import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.platform.autocode.service.AutocodeService;
import com.dalin.platform.autocode.vo.EditMenuTreeVo;
import com.dalin.platform.loa.LoaConst;
import com.dalin.platform.loa.condition.LoaProjectCondition;
import com.dalin.platform.loa.condition.MenuCondition;
import com.dalin.platform.loa.model.LoaMenuEntity;
import com.dalin.platform.loa.model.LoaProjectEntity;
/*****************************************************************************
 * @content: 类信息                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-06-18                                                     *
 * @modDate: 2010-06-18                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class MenuQueryAllAction extends StrutsTwoAction{
	private AutocodeService autocodeService; // 
	private String parentId;
	private Integer deep;

	public String execute() throws Exception {
        super.printInfo("", "===== this action begin");
        List<EditMenuTreeVo> rtnList = new ArrayList<EditMenuTreeVo>();
        // 工程
        if (LoaConst.BASE_PARENT_ID.toString().equals(parentId)) {
        	LoaProjectCondition projectCond = new LoaProjectCondition();
        	List<LoaProjectEntity> projectEntyList = autocodeService
        			.queryListByCond(projectCond.prepareParams());
        	EditMenuTreeVo vo = null;
        	for (LoaProjectEntity enty : projectEntyList) {
        		vo = new EditMenuTreeVo();
        		vo.setId(enty.getProjectId() + "~-"+enty.getProjectId());
        		vo.setText(enty.getProjectName());
        		vo.setDeep(1);
        		rtnList.add(vo);
        	}
        	setResContent(rtnList);
        	return null;
        }
        
        // 工程下面的菜单
        String projectId = parentId.split("~")[0];
        parentId = parentId.split("~")[1];
        MenuCondition menuCondition = new MenuCondition();
    	menuCondition.setProjectId(Long.parseLong(projectId));
    	menuCondition.setParentId(Long.parseLong(parentId));
		List<LoaMenuEntity> menuEntyList = autocodeService.queryListByCond(menuCondition.prepareParams());
		EditMenuTreeVo vo = null;
		for (LoaMenuEntity enty : menuEntyList) {
			vo = new EditMenuTreeVo();
			vo.setId(enty.getProjectId() + "~" + enty.getMenuId());
			vo.setParentId(enty.getProjectId() + "~" + enty.getParentId()+"");
			vo.setHref(enty.getLinkPath());
			vo.setText(enty.getMenuName());
			vo.setMenuValue(enty.getMenuValue());
			vo.setRemark(enty.getRemark());
			
			menuCondition.setProjectId(Long.parseLong(projectId));
			menuCondition.setParentId(enty.getMenuId());
			List<LoaMenuEntity> childList = autocodeService.queryListByCond(menuCondition.prepareParams());
			if (StringUtils.isBlank(childList)) {
				vo.setLeaf(true);
			}
			rtnList.add(vo);
		}
        if (!"1".equals(projectId) && deep == 1) {
        	EditMenuTreeVo platTree = new EditMenuTreeVo();
			platTree.setId(projectId + "~-1");
			platTree.setText("cutePlat平台");
			rtnList.add(platTree);
        }
        setResContent(rtnList);
        super.printInfo("", "===== this action end");
        return null;
    }






	public AutocodeService getAutocodeService() {
		return autocodeService;
	}






	public void setAutocodeService(AutocodeService autocodeService) {
		this.autocodeService = autocodeService;
	}

	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	public Integer getDeep() {
		return deep;
	}
	public void setDeep(Integer deep) {
		this.deep = deep;
	}
}
